﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;
using System.Xml.Linq;
using System.Data;
using System.Device.Location;
using Roteirização;

namespace Roteirização
{
    public partial class Processamento : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        public double getDistance(GeoCoordinate p1, GeoCoordinate p2)
        {
            double d = p1.Latitude * 0.017453292519943295;
            double num3 = p1.Longitude * 0.017453292519943295;
            double num4 = p2.Latitude * 0.017453292519943295;
            double num5 = p2.Longitude * 0.017453292519943295;
            double num6 = num5 - num3;
            double num7 = num4 - d;
            double num8 = Math.Pow(Math.Sin(num7 / 2.0), 2.0) + ((Math.Cos(d) * Math.Cos(num4)) * Math.Pow(Math.Sin(num6 / 2.0), 2.0));
            double num9 = 2.0 * Math.Atan2(Math.Sqrt(num8), Math.Sqrt(1.0 - num8));
            return (6376500.0 * num9);
        }

        public static double Calculate(double sLatitude, double sLongitude, double eLatitude,
                               double eLongitude)
        {
            var sLatitudeRadians = sLatitude * (Math.PI / 180.0);
            var sLongitudeRadians = sLongitude * (Math.PI / 180.0);
            var eLatitudeRadians = eLatitude * (Math.PI / 180.0);
            var eLongitudeRadians = eLongitude * (Math.PI / 180.0);

            var dLongitude = eLongitudeRadians - sLongitudeRadians;
            var dLatitude = eLatitudeRadians - sLatitudeRadians;

            var result1 = Math.Pow(Math.Sin(dLatitude / 2.0), 2.0) +
                          Math.Cos(sLatitudeRadians) * Math.Cos(eLatitudeRadians) *
                          Math.Pow(Math.Sin(dLongitude / 2.0), 2.0);

            // Using 3956 as the number of miles around the earth
            var result2 = 3956.0 * 2.0 *
                          Math.Atan2(Math.Sqrt(result1), Math.Sqrt(1.0 - result1));

            return result2;
        }

        protected void btnProcessa_Click(object sender, EventArgs e)
        {
            //var request = WebRequest.Create("http://maps.googleapis.com/maps/api/distancematrix/xml?origins=Rua+Manuel+Dutra+312+SaoPaulo&destinations=Rua+Orissanga+26+Sao+Paulo|Rua+Consola%C3%A7%C3%A3o+123+Sao+Paulo|06795+005|Rua+Vergueiro+200+Sao+Paulo&mode=driving&language=pt-br&sensor=false");
            //var response = request.GetResponse();
            //var xdoc = XDocument.Load(response.GetResponseStream());
            Service url = new Service();
            DataSet ds = new DataSet();
            //ds.ReadXml("http://maps.googleapis.com/maps/api/distancematrix/xml?origins=Rua+Manuel+Dutra+312+SaoPaulo&destinations=Rua+Orissanga+26+Sao+Paulo|Rua+Consola%C3%A7%C3%A3o+123+Sao+Paulo|06795+005|Rua+Vergueiro+200+Sao+Paulo&mode=driving&language=pt-br&sensor=false");

            ds.ReadXml(url.UrlGoogle("19/06/2013"));

            //Double dist;
            //Double lat1, lat2, long1, long2;
            //lat1 = -23.608764;
            //lat2 = -23.555215;
            //long1 = -46.636632;
            //long2 = -46.646095;

            //dist = Calculate(lat1, long1, lat2, long2);

            //var sCoord =  new  GeoCoordinate ( -23.608764 , -46.636632 ); 
            //var eCoord =  new  GeoCoordinate ( -23.555215 , -46.646095 );
            //dist = sCoord.GetDistanceTo(eCoord);
            //dist = getDistance(sCoord, eCoord);


        }

    }
}


